//
//  WWRelaxCollectionCell.m
//  YiQiTingShu
//
//  Created by WeiWei on 15/11/10.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WWRelaxCollectionCell.h"
#import "WWComicTableCell.h"
#import "WWComicModel.h"
#import "PublicHeader.h"
#import <MBProgressHUD.h>
#import <MJRefresh.h>

@interface WWRelaxCollectionCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) MBProgressHUD *progressView;
@property (nonatomic, assign) NSInteger page;
@end

@implementation WWRelaxCollectionCell

- (void)handleData {
    [AFTools getDataByURLString:@"http://s.budejie.com/topic/list/jingxuan/10/baisishequ-iphone-3.7/0-20.json?appname=baisishequ&asid=E1922CE5-ECA3-494B-9B6A-B637D35AFDC2&client=iphone&device=ios%20device&from=ios&jbk=0&mac=&market=&openudid=6e137d627e2a04769e3a6dc8f94274c404e98ba8&udid=&ver=3.7" WithDataBlock:^(id data) {
        NSArray *tempArray = [NSArray arrayWithArray:[data objectForKey:@"list"]];
        for (NSDictionary *dic in tempArray) {
            WWComicModel *model = [WWComicModel wwComicModelWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        //停止加载
//        [self.tableView.mj_footer endRefreshing];
        //停止加载中提示视图
        [_progressView removeFromSuperview];
    }];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.progressView = [[MBProgressHUD alloc]initWithView:self.contentView];
        _progressView.labelText = @"努力加载中,请稍等";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        _progressView.backgroundColor = [UIColor colorWithRed:0.58 green:0.6 blue:0.61 alpha:0.3];
        
        _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        _page = 1;
        [self handleData];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[WWComicTableCell class] forCellReuseIdentifier:@"cell"];
        self.dataArray = [NSMutableArray array];
        [self.tableView addSubview:_progressView];
        [self.tableView NightWithType:UIViewColorTypeNormal];
        [self.contentView addSubview:_tableView];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            //清空原来的数据
            [self.dataArray removeAllObjects];
            //蒋页码重新设置为1
            _page = 1;
            //重新网络请求
            [self handleData];
        }];
        
        
        //上拉加载
//        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//            //页码加一
//            _page++;
//            [self handleData];
//            
//        }];

    }
    return self;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WWComicTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (self.dataArray.count) {
        WWComicModel *model = self.dataArray[indexPath.row];
        cell.model = model;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataArray.count) {
        WWComicModel *model = self.dataArray[indexPath.row];
        if ([model.type isEqualToString:@"gif"]) {
            
            return ([[model.gif objectForKey:@"height"] integerValue] + 70);
        } else {
            NSInteger h = [[model.image objectForKey:@"height"] integerValue] / 2;
            return (h + 150);
        }

    }
    return 0;
}

@end
